package com.wyh.utils.sqlUtils.announcement;

import com.wyh.entity.Announcements;

import java.util.ArrayList;
import java.util.List;

public class addAnnouncementSqlDynamic {
    public String addDynamic(Announcements announcements) {
        StringBuilder builder = new StringBuilder();
        StringBuilder parameterPlaceholders = new StringBuilder();
        List<String> parameters = new ArrayList<>();
        if (announcements.getName()!=null){
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("name ");
            parameterPlaceholders.append("#{name}");
            parameters.add("name");
        }
        if (announcements.getInformation()!=null){
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("information ");
            parameterPlaceholders.append("#{information}");
            parameters.add("information");
        }
        if (announcements.getType()!=null){
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("type ");
            parameterPlaceholders.append("#{type}");
            parameters.add("type");
        }
        if (announcements.getTime()!=null){
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("time ");
            parameterPlaceholders.append("#{time}");
            parameters.add("time");
        }
        if (announcements.getRemark()!=null){
            if (builder.length() > 0) {
                builder.append(", ");
                parameterPlaceholders.append(", ");
            }
            builder.append("remark ");
            parameterPlaceholders.append("#{remark}");
            parameters.add("remark");
        }

        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO announcements (");
        sql.append(builder);
        sql.append(") VALUES (");
        sql.append(parameterPlaceholders);
        sql.append(");");
        System.out.println(sql);
        return sql.toString();
    }
}
